package org.jeecg.modules.zhouAdmin.admin.goodsCenter.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.mapper.MyMlccThirdFabricorgoodsMapper;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.mapper.MyMlccThirdGoodsMapper;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.pojo.MlccThirdFabricorgoods;
import org.jeecg.modules.zhouAdmin.admin.goodsCenter.pojo.MlccThirdGoods;
import org.jeecg.modules.zhoupackage.mapper.MlccFabricInfoMapper;
import org.jeecg.modules.zhoupackage.mapper.MyMlccCategoryInfoMapper;
import org.jeecg.modules.zhoupackage.mapper.MyMlccProductInfoMapper;
import org.jeecg.modules.zhoupackage.pojo.MlccCategoryInfo;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricInfo;
import org.jeecg.modules.zhoupackage.pojo.MlccProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 工具类
 */
@Component
public class GoodsUtil {
    @Autowired
    MyMlccThirdFabricorgoodsMapper tongMapper;

    @Autowired
    MyMlccThirdGoodsMapper goodsMapper;

    @Autowired
    MlccFabricInfoMapper fabricInfoMapper;

    @Autowired
    MyMlccProductInfoMapper productInfoMapper;

    @Autowired
    MyMlccCategoryInfoMapper categoryInfoMapper;

    /**
     * 想统合表添加记录 (废弃)
     *
     * @param goodsid 商品或面料id
     * @param type    默认0商品，1面料
     */
    @Deprecated
    public void addTonghe(String goodsid, Integer type) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        MlccThirdFabricorgoods fabricorgoods = new MlccThirdFabricorgoods();
        fabricorgoods.setId(UUID.randomUUID().toString()).setType(type).setHybridId(goodsid)
                .setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
        if (type == 0) {
            MlccThirdGoods goods = goodsMapper.selectById(goodsid);
            if (goods != null) {
                fabricorgoods.setGoodsCode(goods.getGoodsCode()).setSupplierGoodsCode(goods.getSupplierGoodsCode())
                        .setGoodsImg(goods.getGoodsMainImg()).setGoodsTypeId(goods.getGoodsTypeId()).setGoodsName(goods.getGoodsName());
            }
        } else if (type == 1) {
            MlccFabricInfo fabricInfo = fabricInfoMapper.selectById(goodsid);
            if (fabricInfo != null) {
                fabricorgoods.setGoodsCode(fabricInfo.getFabricCode()).setSupplierGoodsCode(fabricInfo.getGysfabriccode())
                        .setGoodsImg(fabricInfo.getFabricImg()).setGoodsTypeId(1 + "");
            }
        }
        tongMapper.insert(fabricorgoods);
    }

    /**
     * 统合表删除记录 （废弃）
     *
     * @param goodsid 商品或面料id
     * @param type    默认0商品，1面料
     */
    public void delTonghe(String goodsid, Integer type) {
        QueryWrapper<MlccThirdFabricorgoods> wrapper = new QueryWrapper<>();
        wrapper.eq("hybrid_id", goodsid).eq("type", type);
        tongMapper.delete(wrapper);
    }

    /**
     * 添加面料的时候向商品表添加记录
     *
     * @param fabricInfo 面料对象
     */
    public void addGoods(MlccFabricInfo fabricInfo) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<MlccThirdGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_code", fabricInfo.getFabricCode());
        int count = goodsMapper.selectCount(wrapper);
        if (count == 0) {
            MlccThirdGoods goods = new MlccThirdGoods();
            goods.setId(fabricInfo.getId() + "").setGoodsCode(fabricInfo.getFabricCode()).setIsGood(1)
                    .setGoodsMainImg(fabricInfo.getFabricImg()).setSupplierId(fabricInfo.getSysuserId())
                    .setSupplierGoodsCode(fabricInfo.getGysfabriccode()).setGoodsSeriesId(fabricInfo.getGroupId())
                    .setGoodsTypeId("1").setSeason(fabricInfo.getSeason()).setCreateBy(user.getRealname())
                    .setUpdateBy(user.getRealname());
            goodsMapper.insert(goods);
        }
    }

    /**
     * 更新面料
     *
     * @param fabricInfo 面料对象
     * @param fabricCode 面料编号
     */
    public void updateGoodsByFabric(MlccFabricInfo fabricInfo, String fabricCode) {
        QueryWrapper<MlccThirdGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_code", fabricCode);
        MlccThirdGoods goods1 = goodsMapper.selectOne(wrapper);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (goods1 != null) {
            goods1.setGoodsCode(fabricInfo.getFabricCode()).setIsGood(1)
                    .setGoodsMainImg(fabricInfo.getFabricImg()).setSupplierId(fabricInfo.getSysuserId())
                    .setSupplierGoodsCode(fabricInfo.getGysfabriccode()).setGoodsSeriesId(fabricInfo.getGroupId())
                    .setGoodsTypeId("1").setSeason(fabricInfo.getSeason()).setCreateBy(user.getRealname())
                    .setUpdateBy(user.getRealname());
            goodsMapper.updateById(goods1);
        }
    }

    /**
     * 删除面料
     *
     * @param id 面料id
     */
    public void delFabric(String id) {
        goodsMapper.deleteById(id);
    }

    /**
     * 面料上架
     *
     * @param id 面料id
     */
    public void upFabric(String id) {
        MlccThirdGoods goods = goodsMapper.selectById(id);
        if (goods != null) {
            goods.setStatus(0);
            goodsMapper.updateById(goods);
        }
    }

    /**
     * 面料下架
     *
     * @param id 面料id
     */
    public void offFabric(String id) {
        MlccThirdGoods goods = goodsMapper.selectById(id);
        goods.setStatus(2);
        goodsMapper.updateById(goods);
    }

    /**
     * 商品上架之后，向产品表添加记录
     *
     * @param goods 商品对象
     */
    public void addXianhuo(MlccThirdGoods goods) {
        // 插入之前先查询，这个商品是否添加过
        QueryWrapper<MlccProductInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsid", goods.getId());
        int count = productInfoMapper.selectCount(wrapper);
        if (count == 0) {
            MlccProductInfo info = new MlccProductInfo();
            info.setId(UUID.randomUUID().toString()).setProductName(goods.getGoodsName())
                    .setProductImg(goods.getGoodsMainImg()).setProductCode(goods.getGoodsCode())
                    .setSex(goods.getSex()).setProductType(1).setGoodsid(goods.getId());
            // 给产品查询版号、品类、性别的类别id
            QueryWrapper<MlccCategoryInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("category_code", goods.getPlateNumber()).eq("is_type", 3);
            MlccCategoryInfo banhao = categoryInfoMapper.selectOne(wrapper1);
            if (banhao != null) {
                String BHID = banhao.getId() + "";
                // 查询品类id
                QueryWrapper<MlccCategoryInfo> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("id", banhao.getTopid()).eq("is_type", 2);
                MlccCategoryInfo pinlei = categoryInfoMapper.selectOne(wrapper2);
                String PLID = pinlei.getId() + "";
                if (goods.getSex() == 0) {
                    info.setCategoryIds(BHID + "," + PLID + ",54");
                } else if (goods.getSex() == 1) {
                    info.setCategoryIds(BHID + "," + PLID + ",55");
                }
                productInfoMapper.insert(info);
            }
        } else {
            // 说明现货已近再产品表中存在，又执行上架操作，需要把状态改为正常
            QueryWrapper<MlccProductInfo> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("goodsid", goods.getId());
            MlccProductInfo info = productInfoMapper.selectOne(wrapper3);
            info.setStatus(0);
            productInfoMapper.updateById(info);
        }
    }

    /**
     * 商品下架或删除的时候，同时删除对应的产品
     *
     * @param goods 商品
     */
    public void delXianhuo(MlccThirdGoods goods) {
        QueryWrapper<MlccProductInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsid", goods.getId()).eq("status", 0);
        MlccProductInfo info = productInfoMapper.selectOne(wrapper);
        if (info != null) {
            info.setStatus(1);
            productInfoMapper.updateById(info);
        }
    }
}
